package com.shanghaionstar.smart;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.smart.adapter.FunPagerAdapter;
import com.shanghaionstar.utils.BusinessUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rengui Lu on 2016/6/12.
 */
public class SmartIntroduceFrg extends BaseFragment implements View.OnClickListener {
    @FindViewById(R.id.title_tv)
    TextView mTitleTv;
    @FindViewById(R.id.back_ib)
    ImageButton mBackIb;
    @FindViewById(R.id.intro_vp)
    ViewPager mIntroVp;


    private List<View> views;
    private BaseActivity mActivity;
    private FunPagerAdapter mPagerAdapter;

    private Integer[] pics = {R.drawable.smart_fun_intro_index, R.drawable.smart_fun_intro_first, R.drawable.smart_fun_intro_second, R.drawable.smart_fun_intro_third};

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_slk_fun_intr;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        views = new ArrayList<View>();
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        //初始化引导图片列表
        for (int i = 0; i < pics.length; i++) {
            ImageView iv = new ImageView(mActivity);
            iv.setLayoutParams(mParams);
            iv.setImageResource(pics[i]);
            views.add(iv);
        }
        mPagerAdapter = new FunPagerAdapter(views);
        mIntroVp.setAdapter(mPagerAdapter);
        mIntroVp.setOnPageChangeListener(pageChangeListener);
        initDots();
        initView();
    }

    private void initView() {
        mTitleTv.setText(R.string.smart_fun_intro);
    }

    private int curPos;

    ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//            if(position == pics.length-1 && positionOffset > 0.5)
//                back();
            curPos = position;
        }

        @Override
        public void onPageSelected(int position) {
            setCurDot(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if(state == 1 && curPos == pics.length-1){
                back();
            }
        }
    };

    private ImageView[] dots;
    private int currentIndex;

    private void initDots() {
        LinearLayout ll = (LinearLayout) mActivity.findViewById(R.id.ll);

        dots = new ImageView[pics.length];

        //循环取得小点图片
        for (int i = 0; i < pics.length; i++) {
            dots[i] = (ImageView) ll.getChildAt(i);
            dots[i].setEnabled(true);//都设为灰色
            dots[i].setOnClickListener(this);
            dots[i].setTag(i);//设置位置tag，方便取出与当前位置对应
        }

        currentIndex = 0;
        dots[currentIndex].setEnabled(false);//设置为白色，即选中状态
    }

    /**
     * 设置当前的引导页
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }

        mIntroVp.setCurrentItem(position);
    }

    /**
     * 这只当前引导小点的选中
     */
    private void setCurDot(int position) {
        if (position < 0 || position > pics.length - 1 || currentIndex == position) {
            return;
        }
        dots[position].setEnabled(false);
        dots[currentIndex].setEnabled(true);
        currentIndex = position;
    }

    @Override
    @OnClick({R.id.back_ib})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_ib:
                BusinessUtil.sendReportByClickType(mActivity, FunctionNameFindId.Smarthome_introduction_return.getStringValue());
                back();
                break;
        }
    }
}
